package advanced.network;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;

/**
 * 借助udp packe的思想来发送数据
 * 就像寄快递一样,
 * 写明包裹的地址, 邮寄的内容
 * 收到包裹的人,拆开就能看到里边的内容
 */
public class TalkServer implements Runnable {
    private DatagramSocket server = null;
    private InetSocketAddress packetAddress = null;
    private boolean flg = true;

    public static void main(String[] args) {
        new Thread(new TalkServer(8785, new InetSocketAddress("localhost", 8787)), "server").start();
        new Thread(new TalkClient(8787, new InetSocketAddress("localhost", 8785)), "client").start();
    }

    public TalkServer(int localPort, InetSocketAddress packetAddress) {
        try {
            server = new DatagramSocket(localPort);
            this.packetAddress = packetAddress;
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (flg) {
            DatagramPacket receivePacket = receiveMsg(server, packetAddress);
            String msg = new String(receivePacket.getData());
            if (msg.contains("三星")) {
                //   此时收到的pocket携带了发送pocket过来的机器的一些信息
                //System.out.println("port= " + packet.getPort());
                byte[] msgBytes = null;
                try {
                    msgBytes = "请输入您想要咨询的关于三星的问题".getBytes("UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                sendMsg(msgBytes, receivePacket.getSocketAddress(), server);
            } else if (msg.contains("bye") || msg.contains("exit")) {
                try {
                    sendMsg("已断开与客户端的链接,谢谢使用".getBytes("utf-8"), receivePacket.getSocketAddress(), server);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            } else {
                byte[] str = null;
                try {
                    str = "您输入了其他信息".getBytes("UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                sendMsg(str, receivePacket.getSocketAddress(), server);
            }

        }
    }

    /**
     * @param data    要发送的数据
     * @param address 收件人的地址
     * @param sender  发送人的信息
     */
    private void sendMsg(byte[] data, SocketAddress address, DatagramSocket sender) {
        DatagramPacket packet = new DatagramPacket(data, 0, data.length, address);
        try {
            sender.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 接收数据
     *
     * @param localClient   本地客户端(接收人)
     * @param packetAddress 指定地址的包裹
     * @return
     */
    private DatagramPacket receiveMsg(DatagramSocket localClient, InetSocketAddress packetAddress) {
        byte[] container = new byte[100];
        DatagramPacket packet = new DatagramPacket(container, 0, container.length, packetAddress);
        try {
            localClient.receive(packet);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return packet;
    }

    public void stop() {
        flg = false;
        if (!server.isClosed()) server.close();
    }

}
